<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>汽车之家论坛</title>
    <link href="css/slide.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="singup/css/style.css"/>
    <link rel="stylesheet" type="text/css" href="css/easydialog.css">
    <link rel="stylesheet" type="text/css" href="css/easyui.css">
    <link rel="stylesheet" type="text/css" href="css/icon.css">
    <link rel="stylesheet" type="text/css" href="css/main.css">
    <link rel="stylesheet" type="text/css" href="css/layui.css">
    <style>
        .post{position: fixed;right: 10px;bottom: 30px;z-index: 9999;}
        .l-btn-text{font-size: 24px;text-align: center;color: red;margin: 0}
    </style>
</head>
<body>
<div class="top">
    <div class="top_main">
        <div class="logo" onclick="javascript:location.href='index.php';">我的汽车之家</div>

            <?php
            if(isset($_SESSION['username']) and $_SESSION['username'] !=""){
            ?>
            <div id="login" data-name="注销">注销</div>
            <?php
            }
            else{
            ?>
            <div id="login" data-name="登录">登录</div>
            <?php
            }
            ?>
        <div class="search">
            <form action="" method="post"><input name="search"><input type="submit" value="论坛搜索" class="btnSearch">
            </form>
        </div>
        <div class="clear"></div>
    </div>
    <?php
    include_once 'top.php'
    ?>

    <?php
    include_once 'info.php';
    ?>
    <div class="mainContent">
        <ul id="realContent">


            <!--<li style="float:left;width:30px;height: 30px;color:gray;cursor:pointer" class="SETTOP"><span data-last="1"
                                                                                                          data-id="74"><img
                    src="img/top.png"></span></li>
            <li class="li_bot">
                <ul class="topic2">
                    <li style="width:50%" class="topicTitle"><a
                            href="http://localhost/2018/small_forum/subtopic.php?id=74">BBBBBBBBBBBBBBBBBBBBBBBBBB</a>
                    </li>
                    <li style="width:15%">版主<br><span class="posttime">2016-12-08 09:31:01</span></li>
                    <li style="width:15%"><span class="replyNum">1</span><br><span class="posttime">11</span></li>
                    <li style="width:15%">版主<br><span class="posttime">2019-11-28 11:25:00</span></li>
                </ul>
                <div class="clear"></div>
            </li>
            <div class="clear"></div>-->

        </ul>
        <div class="count">本坛帖子总数：<span id="total1"></span>
            <span id="showNewPost">
                    </span></div>
        <!--<div class="newPost" onclick="getMore();">查看更多</div>-->
    </div>
    <div class="clear"></div>
        <!--<div class="more" onclick="more()" id="more" style=" cursor: pointer; font-size: 16px; line-height: 25px; color: #ffffff; padding: 5px; background-color: #aa5500;  display: block; width: 130px;margin: 15px auto; text-align: center; ">点击加载更多</div>-->

    <div class="bot">版权所有 汽车之家</div>
    <div class="post"><a id="btn" href="#" class="easyui-linkbutton" data-options="{iconCls:'icon-search',width:'150px',height:'50px'}">发新贴</a></div>
    <div id="registerForm" class="form_wrapper" style="width: 550px;">
        <!--<form class="register">-->
            <h3>用户注册</h3>
            <div class="column">
                <div>
                    <label>用户名:</label>
                    <input type="text" name="username" id="username">
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>昵称:</label>
                    <input type="text" name="nickname" id="nickname">
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>出生日期:</label>
                    <div class="birthday">
                        <input type="text"  id="birthday" class="datebox-f combo-f textbox-f" textboxname="birthday" style="display: none;" comboname="birthday">
                    </div>
                </div>
            </div>
            <div class="column">
                <div>
                    <label>选择头像:</label>
                    <form id="uploadPic" action="uploadPic.php" method="post" ecntype="mulitpart/form-data">
                        <input type="file" name="pic" id="pic">
                    </form>
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>电子信箱:</label>
                    <input type="text" name="email" id="email">
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>密码:</label>
                    <input type="password" name="pw" id="pw">
                    <span class="error">This is an error</span>
                </div>
            </div>
            <div class="bottom">
                <div class="remember"></div>
                <input type="button" value="注册" id="singup">
                <a href="javascript:void(0);" rel="login" class="linkform" id="showLogin">已经有帐号了? 点击登录</a>
                <div class="clear"></div>
            </div>
            <div class="clear"></div>
        <!--</form>-->
        <div class="clear"></div>
    </div>
    <div class="clear"></div>
    <div id="loginFrom" class="form_wrapper">
        <form class="login">
            <h3>用户登录</h3>
            <div>
                <label>用户名:</label>
                <input type="text" id="username2">
                <span class="error">This is an error</span>
            </div>
            <div>
                <label>密码: <a href="singup/forgot_password.html" rel="forgot_password"
                              class="forgot linkform">忘记密码?</a></label>
                <input type="password" id="pw2">
                <span class="error">This is an error</span>
            </div>
            <div class="bottom">
                <div class="remember"><input type="checkbox" value="1"  name="rememberMe"><span>保存登录状态</span></div>
                <input type="button" value="登录" onclick="login();">
                <a href="javascript:void(0);" rel="register" class="linkform" id="showRegister">还没有帐号? 点击注册</a>
                <div class="clear"></div>
            </div>
        </form>
    </div>
    <div class="clear"></div>
    <script src="js/jquery.min.js"></script>
    <script src="js/jquery.form.min.js"></script>
    <script src="js/jquery.easyui.min.js"></script>
    <script src="js/responsiveslides.min.js"></script>
    <script src="js/easydialog.min.js"></script>
    <script src="js/easyui-lang-zh_CN.js"></script>
    <script src="js/site.js"></script>
    <script src="layer/layer/layer.js"></script>
    <script>
        let perPage = 5,page = 1,working = 0,noData = 0;
        function login(){
            if($("#username2").val().trim() == "" || $("#pw2").val().trim() == ""){
                alert("用户名和密码必须填写！");
                return;
            }
            $.ajax({
                url:'postLogin.php',
                data:{
                    username:$("#username2").val().trim(),
                    pw:$("#pw2").val().trim(),
                    action:'0'//action:0表示手动登录
                },
                type:'post',
                dataType:'json',
                success:function (d) {
                    if(d.error == 1){
                        alert(d.errMsg);
                    }else{
                        //登录成功，判断用户是否有勾选“保存登录”选项。如果有，则使用localstorage保存登录信息
                        if($('input[name="rememberMe"]:checked').val() == 1){
                            localStorage.setItem('rememberMe',1);
                            localStorage.setItem('username',$("#username2").val().trim());
                        }
                        else{
                            localStorage.setItem('rememberMe','');
                            localStorage.setItem('username','');
                        }
                        easyDialog.close();
                        //更新info里面的各项内容
                        getInfo();
                        $(".info").show();
                    }
                },
                error:function () {
                    alert('网络错误');
                }
            })
        }
        function getInfo(){
            $.ajax({
                url:'getInfo.php',
                dataType:'json',
                success:function (d) {
                    if(d.error == 1){
                        alert(d.errMsg);
                    }
                    else{
                        $("#tx1").attr("src","tx/"+d.info['pic']);
                        $("#username1").text(d.info['username']);
                        $("#nickname1").text(d.info['nickname']);
                        $("#singUpDate").text(d.info['postDate']);
                        $("#total").text(d.info['nb']);
                        $("#lastPostTime").text(d.info['lp']);
                        $("#lastTitle").text(d.info['title']);
                        $("#lastReplyTime").text(d.info['lastreplytime']);
                        //登录成功，在右上角显示“注销”
                        $("#login").attr('data-name','注销').text('注销');
                    }
                },
                error:function () {

                }
            })
        }
        function  more() {
            page++;
            getMainTopic(page,perPage);
        }
        function total() {
            $.ajax({
                url:'getTotal.php',
                dataType:'json',
                success:function (d) {
                    $("#total1").text(d.numbers);
                }
            })
        }
        function getMainTopic(page1,perPage1){
            //获取首页列表数据
            $.ajax({
                url:'getMaintopic.php',
                data:{page:page1,perPage:perPage1},
                type:'post',
                dataType:'json',
                beforeSend:function(){
                  //显示一个等待动画
                },
                success:function (d) {
                    if(d.error == 1){
                        if(page>1){
                            page--;
                        }
                        noData = 1;//表示 没有数据了
                        alert(d.errMsg);
                    }
                    else{
                        let s = ''
                        if(page == 1){
                            s = '<li>\n' +
                                '                <ul class="topic1">\n' +
                                '                    <li style="float: left;width: 30px;"><img src="img/topnull.png"></li>\n' +
                                '                    <li style="width:50%"><b>帖子标题</b></li>\n' +
                                '                    <li style="width:15%"><b>作者/发布时间</b></li>\n' +
                                '                    <li style="width:15%"><b>回复/点击</b></li>\n' +
                                '                    <li style="width:15%"><b>最后回复</b></li>\n' +
                                '\n' +
                                '                </ul>\n' +
                                '                <div class="clear"></div>\n' +
                                '            </li>';
                        }
                        for(var i=0;i<d.info.length;i++){
                            s += '<li style="float:left;width:30px;height: 30px;color:gray;cursor:pointer" class="SETTOP">';
                            if(d.info[i]['istop'] == 1){
                                s += '<span data-last="1" data-id="'+d.info[i]['id']+'"><img src="img/top.png"></span></li>';
                            }
                            else{
                                s += '<span data-last="0" data-id="'+d.info[i]['id']+'">顶</span></li>';
                            }
                            s+='<li class="li_bot">';
                            s+='<ul class="topic2"><li style="width:50%" class="topicTitle">';
                            s += '<a href="subtopic.php?id='+d.info[i]['id']+'">'+d.info[i]['title']+'</a></li> <li style="width:15%">'+d.info[i]['postNickName']+'<br><span class="posttime">'+d.info[i]['posttime']+'</span></li><li style="width:15%"><span class="replyNum">'+d.info[i]['replyNumbers']+'</span><br><span class="posttime">'+d.info[i]['hits']+'</span></li><li style="width:15%">'+d.info[i]['replyNickName']+'<br><span class="posttime">'+d.info[i]['lastReplyTime']+'</span></li></ul><div class="clear"></div></li><div class="clear"></div>';
                        }
                        $("#realContent").append(s);
                        $(".SETTOP span").unbind('click');//取消点击事件的绑定
                        $(".SETTOP span").click(function () {//重新绑定点击事件
                            //console.log($(this).attr('data-id'));
                            //console.log($(this).attr('data-last'));
                            var current = $(this);
                            $.ajax({
                                url:'settop.php',
                                data:{id:current.attr('data-id'),last:current.attr('data-last')},
                                type:'post',
                                dataType:'json',
                                success:function (d) {
                                    //console.log(d);
                                    if(d.error==1){
                                        alert(d.errMsg);
                                    }
                                    else{
                                        //更改图标状态
                                        console.log(current.attr('data-last'));
                                        if(current.attr('data-last') == 1){
                                            //取消置顶
                                            current.attr('data-last',0);
                                            current.html('顶');
                                        }
                                        else{
                                            //加上置顶
                                            current.attr('data-last',1);
                                            current.html('<img src="img/top.png">');
                                        }
                                    }
                                },
                                error:function () {

                                }
                            })
                        })
                    }
                    working = 0;//表示请求结束
                },
                error:function () {
                    alert('网络错误');
                }

            })
        }
        $(function () {
            //先判断当前是否登录。如果登录了，则显示info区域
            <?php
                if(isset($_SESSION['username']) and $_SESSION['username'] != ""){
                    //说明已经处于登录状态
                    ?>
                getInfo();
                $(".info").show();
            <?php
                }
                else{
                    //说明未登录，则判断是否有自动登录
                    echo "autoLogin();";
                }
            ?>
            //再判断当前是否保存了上一次的登录状态，如果有登录状态，则需要模拟登录
            function autoLogin(){
                if(localStorage.getItem('rememberMe') == 1){
                    $.ajax(
                        {
                            url:'postLogin.php',
                            data:{
                                username:localStorage.getItem('username'),
                                action:'1'},//action:1表示自动登录
                            type:'post',
                            dataType:'json',
                            success:function (d) {
                                if(d.error == 0){
                                    getInfo();
                                    $(".info").show();
                                }
                                else{
                                    localStorage.setItem('rememberMe',0);
                                    localStorage.setItem('username','');
                                }
                            }
                        }
                    )
                }
            }
            $("#singup").click(function () {
                let errLog = 0, errMsg = '';
                if($("#username").val().trim() == ""){
                    errLog = 1;
                    errMsg += "用户名必须填写！\n";
                }
                if($("#pw").val().trim() == ""){
                    errLog = 1;
                    errMsg += "密码必须填写！\n";
                }
                if($("#nickname").val().trim() == ""){
                    errLog = 1;
                    errMsg += "昵称必须填写！\n";
                }
                if($("#birthday").datebox('getValue') == ""){
                    errLog = 1;
                    errMsg += "生日必须选择！\n";
                }
                if($("#pic").val().trim() == ""){
                    errLog = 1;
                    errMsg += "必须上传头像！\n";
                }
                if(errLog == 1){
                    alert(errMsg);
                }
                else{
                    //各项数据填写完整，可以开始注册
                    singup();
                }
            })
            function singup(){
               $("#uploadPic").ajaxSubmit({
                   /*url:"uploadPic.php",
                   type:'post',*/
                   dataType:"json",
                   success:function (d) {
                        if(d.error == 1){
                            alert(d.errMsg);
                        }
                        else{
                            //说明头像上传OK，可以注册，写入数据库
                            $.ajax({
                                url:'postSingup.php',
                                data:{
                                    username:$("#username").val().trim(),
                                    pw:$("#pw").val().trim(),
                                    nickname:$("#nickname").val().trim(),
                                    birthday:$("#birthday").datebox('getValue'),
                                    email:$("#email").val().trim(),
                                    filename:d.fileName
                                },
                                dataType: 'json',
                                type:'post',
                                success:function (d) {
                                    if(d.error ==1){
                                        alert(d.errMsg);
                                    }
                                    else{
                                        alert('注册成功');
                                        easyDialog.close();
                                        $("#login").trigger('click');
                                    }
                                },
                                error:function () {

                                }
                            })
                        }
                        $("#uploadPic").resetForm();
                   },
                   error:function () {
                    alert(11111);
                   }
               })
            }
            $("#birthday").datebox({
                required: true,
                panelWidth: 200,
                panelHeight: 250
            })
            $("#_easyui_textbox_input1").focus(function () {
                $("#_easyui_textbox_input1").blur();
            })
            $("#slider").responsiveSlides({
                speed: 1000
            });

            $("#login").click(function () {
                if($(this).attr('data-name') == '注销'){
                    location.href='logout.php';
                }
                else{
                    easyDialog.open({
                        container: 'loginFrom',
                        fixed: false
                    })
                }

            })
            $("#showRegister").click(function () {
                easyDialog.close();
                easyDialog.open({
                    container: 'registerForm',
                    fixed: false
                })
            })
            $("#showLogin").click(function () {
                easyDialog.close();
                easyDialog.open({
                    container: 'loginFrom',
                    fixed: false
                })
            })
            getMainTopic(page,perPage);
            total();
            $("#btn").click(function () {
                layer.open({
                    type: 2,
                    title:'发新贴',
                    area: ['900px', '640px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: 'ueditor/index.html'
                });
            })
            $(window).scroll(function () {  //当滚动的时候
                //console.log("1-window height:"+$(window).height());
                //console.log("2-document height:"+$(document).height());
                //console.log("3-window scrollTop:"+$(window).scrollTop());
                var w_height = parseInt($(window).height());
                var d_height = parseInt($(document).height());
                var s_height = parseInt($(window).scrollTop());
                if(d_height - (w_height + s_height) <= 10){
                    if(working == 0 && noData == 0){ //说明当前未请求数据，且还有数据
                        working = 1;//表明正在请求数据
                        more();
                    }

                }
            })

        })

    </script>

</div>


</body>
</html>